package com.yxx.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxx.entity.Album;
import com.yxx.entity.Photo;
import com.yxx.entityModel.vo.Result.PageResult;
import com.yxx.entityModel.vo.query.PhotoQuery;
import com.yxx.entityModel.vo.request.PhotoInfoRequest;
import com.yxx.entityModel.vo.request.PhotoRequest;
import com.yxx.entityModel.vo.response.AlbumBackResponse;
import com.yxx.entityModel.vo.response.PhotoBackResponse;
import com.yxx.entityModel.vo.response.PhotoResponse;
import com.yxx.enums.FilePathEnum;
import com.yxx.mapper.AlbumMapper;
import com.yxx.mapper.PhotoMapper;
import com.yxx.service.BlogFileService;
import com.yxx.service.PhotoService;
import com.yxx.strategy.context.UploadStrategyContext;
import com.yxx.utils.BeanCopyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author yuxiangxun
 * @date 2025/2/1
 * @apiNote
 */
@Service
@Slf4j
public class PhotoServiceImpl extends ServiceImpl<PhotoMapper, Photo> implements PhotoService{

    @Resource
    private PhotoMapper photoMapper;

    @Resource
    private AlbumMapper albumMapper;

    @Resource
    private UploadStrategyContext uploadStrategyContext;

    @Resource
    private BlogFileService blogFileService;

    /**
     * 查看后台照片列表
     * @param photoQuery
     * @return
     */
    @Override
    public PageResult<PhotoBackResponse> PhotoBackListVO(PhotoQuery photoQuery) {
        // 查询照片数量
        Long count = photoMapper.selectCount(new LambdaQueryWrapper<Photo>()
                .eq(Objects.nonNull(photoQuery.getAlbumId()), Photo::getAlbumId, photoQuery.getAlbumId()));
        // 如果照片数量为0，则new一个分页结果对象并返回
        if(count == 0){
            return new PageResult<>();
        }
        // 如果照片数量不为0，则分页查询照片列表信息并返回
        List<PhotoBackResponse> photoBackList = photoMapper.selectBackPhotoListVO(photoQuery);
        return new PageResult<>(photoBackList,count);
    }

    /**
     * 查看照片相册信息
     * @param albumId
     * @return
     */
    @Override
    public AlbumBackResponse getAlbumInfo(Integer albumId) {
        // 根据相册id查询相册信息
        AlbumBackResponse albumBackResponse = albumMapper.selectAlbumInfoById(albumId);
        // 如果查询出来的相册信息为空，则返回null
        if(Objects.nonNull(albumBackResponse)){
            return null;
        }
        // 如果查询出来的相册信息不为空，则根据相册id查询照片数量
        Long photoCount = photoMapper.selectCount(new LambdaQueryWrapper<Photo>().eq(Photo::getAlbumId,albumId));
        albumBackResponse.setPhotoCount(photoCount);
        return albumBackResponse;
    }

    /**
     * 上传照片
     * @param file
     * @return
     */
    @Override
    public String uploadPhoto(MultipartFile file) {
        String url = uploadStrategyContext.executeUploadStrategy(file, FilePathEnum.PHOTO.getPath());
        blogFileService.saveBlogFile(file,url,FilePathEnum.PHOTO.getFilePath());
        return url;
    }

    /**
     * 批量添加照片
     * @param photoRequest
     */
    @Override
    public void addPhoto(PhotoRequest photoRequest) {
        // 根据照片url列表创建照片对象列表
        List<Photo> photoList = photoRequest.getPhotoUrlList().stream()
                .map(url -> Photo.builder()
                        .albumId(photoRequest.getAlbumId())
                        .photoName(IdWorker.getIdStr())
                        .photoUrl(url)
                        .build())
                .collect(Collectors.toList());
        this.saveBatch(photoList);
    }

    /**
     * 修改照片信息
     * @param photoInfoRequest
     */
    @Override
    public void updatePhoto(PhotoInfoRequest photoInfoRequest) {
        // 对象转换
        Photo photo = BeanCopyUtils.copyBean(photoInfoRequest, Photo.class);
        baseMapper.updateById(photo);
    }

    /**
     * 删除照片
     * @param photoIdList
     */
    @Override
    public void deletePhoto(List<Integer> photoIdList) {
        baseMapper.deleteBatchIds(photoIdList);
    }

    /**
     * 移动照片
     * @param photoRequest
     */
    @Override
    public void movePhoto(PhotoRequest photoRequest) {
        // 将获取到的PhotoIdList转换成Photo对象列表
        List<Photo> photoList = photoRequest.getPhotoIdList().stream()
                .map(photoId -> Photo.builder()
                        .id(photoId)
                        .albumId(photoRequest.getAlbumId())
                        .build())
                .collect(Collectors.toList());
        // 批量新增操作
        this.updateBatchById(photoList);
    }

    /**
     * 查看照片列表
     * @param albumId
     * @return
     */
    @Override
    public Map<String, Object> photoListVO(Integer albumId) {
        Map<String,Object> resultMap = new HashMap<>(2);
        // 根据 albumId 查询相册信息 -> 得到该相册名称
        String albumName = albumMapper.selectOne(new LambdaQueryWrapper<Album>()
                    .select(Album::getAlbumName)
                    .eq(Album::getId, albumId))
                .getAlbumName();
        // 根据 albumId 查询照片列表 -> 得到该相册下的照片列表
        List<PhotoResponse> photoVOList = photoMapper.selectPhotoVOList(albumId);
        resultMap.put("albumName",albumName);
        resultMap.put("photoVOList",photoVOList);
        return resultMap;
    }
}
